﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data.Odbc;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Interfaces;

namespace AliorBank
{
    [Export(typeof(ICsvFile))]
    public class AliorBankCsvFile : CsvFileBase
    {
        public AliorBankCsvFile()
        {
            Transactions = new List<Transaction>();
            BankName = "AliorBank";
        }

        public override void ReadFromCsv(string path)
        {
            Transactions.Clear();
            using (StreamReader reader = new StreamReader(path, Encoding.Default))
            {
                string line = reader.ReadLine();
                bool firstLine = true;
                while (string.IsNullOrWhiteSpace(line) == false)
                {
                    if (firstLine == false)
                    {
                        string[] parts = line.Split(new[] { ';' });
                        Transaction transaction = new Transaction();
                        transaction.Date = DateTime.ParseExact(parts[0], "yyyyMMdd", null);
                        transaction.PayMode = ParsePayMode(parts[parts.Length - 4]);
                        transaction.Info = string.Format("{0} {1} {2} {3}", parts[4], parts[5], parts[6], parts[7]).Replace("\"", string.Empty);
                        transaction.Memo = transaction.Info;
                        transaction.PayeeName = GetPayeeName(parts);
                        transaction.Amount = decimal.Parse(parts[parts.Length - 3]);
                        transaction.Tags.Add(DateTime.Now.AddMonths(-1).ToString("MM-yyyy"));
                        Transactions.Add(transaction);
                    }
                    else
                    {
                        firstLine = false;
                    }
                    line = reader.ReadLine();
                }
            }
        }

        private static string GetPayeeName(string[] parts)
        {
            if (string.IsNullOrWhiteSpace(parts[3].Replace("\"", string.Empty)))
            {
                int lastIndex = parts[5].LastIndexOf(",");
                if (lastIndex != -1 && parts[5].Length > lastIndex)
                {
                    return parts[5].Substring(lastIndex + 1).Replace("\"", string.Empty);
                }
                return null;
            }
            return parts[3].Replace("\"", string.Empty);
        }

        private PayMode ParsePayMode(string payMode)
        {
            payMode = payMode.Replace("\"", string.Empty);
            switch (payMode)
            {
                case "Transakcjakartą debetową":
                    return PayMode.DebitCard;
                case "Przelew":
                    return PayMode.MoneyTransfer;
                case "Przelew na rachunki w innym banku":
                    return PayMode.MoneyTransfer;
                default:
                    return PayMode.None;
            }
        }
    }
}
